package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.mapper.LogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * 日志消息监听类
 * 将app操作日志消息 写入tb_log表中
 */
@Component
@RocketMQMessageListener(
        topic = "tanhua-logs",consumerGroup = "mytanhualogs"
)
@Slf4j
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogMapper logMapper;

    /**
     * 以后工作中重点-业务处理
     * @param message
     */
    @Override
    public void onMessage(String message) {
        //消息数据 将string转为map
        Map<String,String> map = JSON.parseObject(message, Map.class);
        String userId = map.get("userId");//userId
        String type = map.get("type");//类型
        String logtime = map.get("logtime");//日志时间

        Log tanhualog = new Log();
        tanhualog.setUserId(Long.parseLong(userId));
        tanhualog.setType(type);
        tanhualog.setLogTime(logtime);
        tanhualog.setPlace("中粮");
        tanhualog.setEquipment("诺基亚黑白");
        tanhualog.setCreated(new Date());//日志创建时间
        logMapper.insert(tanhualog);
        log.debug("**************日志保存成功了************",tanhualog.toString());
    }
}
